package com.ruoyi.common.vip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.business.entity.YytMember;
import com.ruoyi.common.business.service.YytMemberService;
import com.ruoyi.common.vip.entity.YytVipMember;
import com.ruoyi.common.vip.dao.YytVipMemberMapper;
import com.ruoyi.common.vip.service.YytVipMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
* <p>
    *  服务实现类
    * </p>
*
* @author MrPing
* @since 2022-07-14
*/
@Service
public class YytVipMemberServiceImpl extends ServiceImpl<YytVipMemberMapper, YytVipMember> implements YytVipMemberService {

    @Autowired
    private YytVipMemberMapper yytVipMemberMapper;

    @Autowired
    private YytMemberService yytMemberService;

    @Override
    public void reduceNumber(String id) {
        YytMember yytMember = yytMemberService.getById(id);
        QueryWrapper<YytVipMember> wrapper = new QueryWrapper<>();
        String endNewTime = String.valueOf(yytMember.getVipEndTime()).replace("T", " ");
        wrapper.eq("member_id", id).eq("vip_end_time", endNewTime).eq("status", "1");
        YytVipMember vipMember = super.getOne(wrapper, false);
        UpdateWrapper<YytVipMember> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", vipMember.getId()).setSql("greens_number = greens_number - 1");
        if(vipMember.getGreensNumber() == 1){
            updateWrapper.set("status", "0");
            UpdateWrapper<YytMember> updateWrapper1 = new UpdateWrapper<>();
            updateWrapper1.eq("id", id).set("is_vip", "0").set("vip_end_time", null);
            yytMemberService.update(updateWrapper1);
        }
        super.update(updateWrapper);
    }
}
